package platform.nio.server;

import platform.common.CommonInfo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.charset.Charset;

public class NioServer{
    Selector selector;
    ServerSocketChannel socket;

    public static void main(String[] args) {
        new NioServer().bind(CommonInfo.port);
    }

    private void bind(int port) {
        try{
            selector =  Selector.open();
            socket = ServerSocketChannel.open();
            socket.configureBlocking(false);
            socket.socket().bind(new InetSocketAddress(port),1024);
            socket.register(selector, SelectionKey.OP_ACCEPT);
            System.out.println("nio启动");
            new NioServerHandler(selector,Charset.forName("utf-8")).start();
        } catch (ClosedChannelException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
